package com.bdqn.zhang.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.bdqn.zhang.entity.Category;
import com.bdqn.zhang.entity.Entry;
import com.bdqn.zhang.entity.EntryEx;
import com.bdqn.zhang.service.EntryService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class EntryAction extends ActionSupport implements ModelDriven<Entry> {

    private Entry entry = new Entry();

    @Override
    public Entry getModel() {
        return entry;
    }


    private EntryService entryService;

    public void setEntryService(EntryService entryService) {
        this.entryService = entryService;
    }

    private String result;

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public Integer page;

    public Integer limit;

    public Long cId;

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public Long getcId() {
        return cId;
    }

    public void setcId(Long cId) {
        this.cId = cId;
    }

    public String tobook() throws Exception {
        return SUCCESS;
    }

    public String listbook(){
        List<Entry> entries = entryService.findAllEntries(cId,page,limit);

        List<EntryEx> entryExs = new ArrayList<>(entries.size());

        if(entries != null && entries.size() > 0){
            for (int i = 0; i < entries.size(); i++) {
                EntryEx entryEx = new EntryEx();
                BeanUtils.copyProperties(entries.get(i), entryEx);
                entryEx.setCategoryName(entries.get(i).getCategory().getName());
                entryExs.add(entryEx);
            }
        }

        //设置过滤器，列出要显示的数据，日期不能转换 ，只能显示单张表的数据
        /*SimplePropertyPreFilter filter = new SimplePropertyPreFilter("id","title","summary","uploaduser","createdate","category");
        result = JSON.toJSONString(entries,filter);*/

        /*下面的方法会报错，不可用*/
        //result = JSON.toJSONString(entries, SerializerFeature.DisableCircularReferenceDetect);


        // 使用扩展实体类的方式接受后台返回查询结果 再转换json， 设置过滤器 排除 非显示的分类对象字段 日期转码在前台实现
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter("id","title","summary","uploaduser","createdate","categoryName");
        result = JSON.toJSONString(entryExs,filter);


        //result = JSON.toJSONStringWithDateFormat(entryExs,"yyyy-MM-dd");
        return SUCCESS;
    }

    public String getEntryCount() throws Exception {
        Long count = entryService.getEntryCount(cId);
        result = JSON.toJSONString(count);
        return SUCCESS;
    }

    /**
     * 该方法应该定义在分类的相关类中，暂放这里
     * @return
     * @throws Exception
     */
    public String getCategory() throws Exception {
        List<Category> categoryList= entryService.getCategory();
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter("id","name");
        result = JSON.toJSONString(categoryList,filter);
        return SUCCESS;
    }
}
